//
//  ChangeItemView.m
//  Learn
//
//  Created by chijianguo on 2024/4/19.
//  Copyright © 2024 迟建国. All rights reserved.
//

#import "ChangeItemView.h"
#import "QuestionTableViewCell.h"
#import "TextbookQuestionsModel.h"

@interface ChangeItemView ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *units;
@property (nonatomic, assign)NSUInteger index;

@end

@implementation ChangeItemView
+ (ChangeItemView *)chooseModulenWithQuestions:(NSArray *)units index:(NSUInteger)index Complection:(void(^)(NSUInteger index))block {
    ChangeItemView *  inputView;
    inputView = [[ChangeItemView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, 480 + SafeAreaBottomHeight)];
    inputView.units = units;
    inputView.index= index;
    [inputView initUI];
    inputView.chooseBlock = block;
    
    NSLog(@"%@", NSObject.getKeyWindowRootViewController);
    [NSObject.getKeyWindowRootViewController gq_presentFromeBottomView:inputView];
    NSObject.getKeyWindowRootViewController.gq_enableBlankDismiss = YES;
    
    return inputView;

}

- (void)initUI {
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, 480 + SafeAreaBottomHeight)];
    bgView.backgroundColor = [UIColor whiteColor];
    [bgView setCornerOnTop:15];
    [self addSubview:bgView];
    
    self.tableView=[[UITableView alloc]initWithFrame:CGRectMake(0,10, SCREEN_W, 400) style:UITableViewStylePlain];
    [bgView addSubview:self.tableView];
    if (@available(iOS 13.0, *)) {
        [self.tableView setAutomaticallyAdjustsScrollIndicatorInsets:NO];
    } else {
        // Fallback on earlier versions
    }
    if (@available(iOS 15.0, *)) {
        self.tableView.sectionHeaderTopPadding = 0;
    } else {
        // Fallback on earlier versions
    }
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    self.tableView.backgroundColor=[UIColor clearColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.showsHorizontalScrollIndicator = NO;
    [self.tableView registerNib:[UINib nibWithNibName:@"QuestionTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"QuestionTableViewCell"];
    
    UIButton *doneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    doneBtn.layer.cornerRadius = 22.5;
    doneBtn.layer.masksToBounds = YES;
    doneBtn.backgroundColor = rgba(41, 241, 156, 1);
    [doneBtn setTitle:@"完成" forState:UIControlStateNormal];
    [doneBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    doneBtn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:18];
    [doneBtn addTarget:self action:@selector(done) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:doneBtn];
    [doneBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgView.mas_left).offset(10);
        make.right.equalTo(bgView.mas_right).offset(-10);
        make.bottom.equalTo(bgView.mas_bottom).offset(-SafeAreaBottomHeight - 10);
        make.height.offset(45);
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.units.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 52;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    QuestionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"QuestionTableViewCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"QuestionTableViewCell" owner:self options:nil] lastObject];
    }
    if(self.index == indexPath.row){
        cell.titleL.textColor = rgbHex(0x29F19C, 1);
    }else {
        cell.titleL.textColor = rgbHex(0x333333, 1);
    }
    cell.titleL.text = [NSString stringWithFormat:@"第%ld大题", (long)(indexPath.row + 1)];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    self.index= indexPath.row;
    [self.tableView reloadData];
}

- (void)done {
    self.chooseBlock(self.index);
    [NSObject.getKeyWindowRootViewController gq_removeBgView];
}

@end
